﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace svg2wkt
{
    public partial class FolderContentsConverter : Form
    {
        private string connectionString = "";
        public FolderContentsConverter()
        {
            InitializeComponent();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About frmAbout = new About();
            frmAbout.ShowDialog();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBoxLogin.Enabled = !checkBoxUseWindowsAuth.Checked;
            textBoxPassword.Enabled = !checkBoxUseWindowsAuth.Checked;
        }

        private void generateConnectionString()
        {
            connectionString = "";
            connectionString += "Data Source=" + textBoxServerName.Text + ";";
            connectionString += "Initial Catalog=Graphs;";
            if (checkBoxUseWindowsAuth.Checked)
                connectionString += "Integrated Security=True";
            else
                connectionString += "user id=" + textBoxLogin.Text + ";password=" + textBoxPassword.Text + ";";
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            generateConnectionString();
            SqlConnection sqlConnection1 = new SqlConnection(connectionString);
            try
            {
                sqlConnection1.Open();
                labelStatus.Text = "Status: Connected";
                Log("Connected successfully");
                sqlConnection1.Close();
            }
            catch (Exception ex)
            {
                labelStatus.Text = "Status: Error";
                Log(ex.ToString());
            }


        }
        private void Log(string logMessage)
        {
            textBoxLog.AppendText(logMessage + "\r\n");
            textBoxLog.ScrollToCaret();
        }
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBoxFoldersList.AppendText(folderBrowserDialog1.SelectedPath + "\r\n");
            }
        }

        private void buttonProcess_Click(object sender, EventArgs e)
        {
            generateConnectionString();
            // Get list of all folders
            List<string> foldersList = new List<string>();
            string[] folders = textBoxFoldersList.Text.Split('\n');
            for (int i = 0; i < folders.Length; i++)
                folders[i] = folders[i].Replace("\r", "").Trim();
            for (int i = 0; i < folders.Length; i++)
                if (folders[i].Length > 0)
                    foldersList.Add(folders[i]);
            // If necessary, get list of all subfolders
            if (checkBoxIncludeSubfolders.Checked)
                for (int i = 0; i < folders.Length; i++)
                    if (folders[i].Length > 0)
                        try
                        {
                            //folders[i] = folders[i].Replace("\r", "");
                            string[] subfolders = Directory.GetDirectories(folders[i], "*", SearchOption.AllDirectories);
                            for (int j = 0; j < subfolders.Length; j++)
                                if (subfolders[j].Length > 0)
                                    foldersList.Add(subfolders[j]);
                        }
                        catch (Exception ex)
                        {
                            Log(ex.ToString());
                            Log(folders[i] + " is not a valid folder!");
                            foldersList.Remove(folders[i]);
                        }
            // Remove all duplicates
            List<string> distinctFoldersList = new List<string>();
            distinctFoldersList = foldersList.Distinct().ToList();
            // Get list of all SVG files, and process all those files
            foreach (string folderName in distinctFoldersList)
            {
                Log("Processing folder: " + folderName);
                try
                {
                    string[] filePaths = Directory.GetFiles(folderName, "*.svg");
                    for (int i = 0; i < filePaths.Length; i++)
                        if (filePaths[i].Length > 0)
                        {
                            //fileList.Add(filePaths[i].Replace("\r", ""));
                            string fileName = filePaths[i].Replace("\r", "");
                            Converter converter = new Converter(fileName);
                            int code = converter.Process();
                            if ((code == 0) && (connectionString != ""))
                            {
                                code = converter.SaveToDatabase(connectionString);
                                Log("Processed file: " + fileName);
                            }
                            if (code != 0)
                                Log(converter.ErrorMessage);
                        }
                }
                catch (Exception ex)
                {
                    Log(ex.ToString());
                }
            }
            Log("Finished!");
        }
    }
}
